﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;
using System.Globalization;

namespace Frontend
{
    class BoolToVisibilityConverter: IValueConverter 
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(Visibility))
            {
                throw new InvalidCastException("BoolToVisibilityConverter converts only to Visibility type");
            }

            bool originalValue = (bool)value;


            //convert bool paramter before using it
            if (parameter != null)
            {
                IValueConverter boolConverter = (IValueConverter)parameter;
                originalValue = (bool)boolConverter.Convert(originalValue,typeof(bool),parameter,culture);
            }
            
            Visibility convertedValue = Visibility.Visible;

            if (originalValue == false)
            {
                convertedValue = Visibility.Collapsed;
            }

            return convertedValue;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(Visibility))
            {
                throw new InvalidCastException("BoolToVisibilityConverter expected only Visibility type as input value");
            }

            Visibility originalValue = (Visibility)value;
            bool convertedValue = true;
            if (originalValue != Visibility.Visible)
            {
                convertedValue = false;
            }

            if (parameter != null)
            {
                IValueConverter boolConverter = (IValueConverter)parameter;
                convertedValue = (bool)boolConverter.ConvertBack(originalValue, typeof(bool), parameter, culture);
            }
            
            return convertedValue;
        }
    }
}
